package com.tedu.studentmanagement.controller;

import com.tedu.studentmanagement.common.result.JsonResult;
import com.tedu.studentmanagement.common.result.StatusCode;
import com.tedu.studentmanagement.mapper.StudentMapper;
import com.tedu.studentmanagement.pojo.dto.StudentLoginDTO;
import com.tedu.studentmanagement.pojo.dto.StudentRegDTO;
import com.tedu.studentmanagement.pojo.entity.Student;
import com.tedu.studentmanagement.pojo.vo.StudentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@RestController
@Api(tags = "学生模块")
@RequestMapping("v1/stu")
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;
    @ApiOperation("学生注册功能")
    @PostMapping("/insert")
    public JsonResult insetStudent(@RequestBody @Validated StudentRegDTO studentRegDTO){
        Student student = new Student();
        StudentVO studentVO = studentMapper.selectStudentById(studentRegDTO.getId());
        if (studentVO==null){
            BeanUtils.copyProperties(studentRegDTO,student);
            studentMapper.insertStudent(student);
            return new JsonResult(StatusCode.OPERATION_SUCCESS);
        }else {
            return new JsonResult();
        }
    }

    @ApiOperation("学生登录功能")
    @PostMapping("/login")
    public JsonResult studentLogin(@RequestBody @Validated StudentLoginDTO studentLoginDTO,@ApiIgnore HttpSession session){
        StudentVO studentVO = studentMapper.selectStudentByIdAndPassword(studentLoginDTO.getId(),studentLoginDTO.getPassword());
        if (studentVO==null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("studentVO",studentVO);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    @ApiOperation("删除学生功能")
    @DeleteMapping("/delete")
    @ApiImplicitParam(name = "id",value = "学生ID",dataType = "String",required = true,example = "20211106208")
    public JsonResult studentDelect(String id){
        int num = studentMapper.delectStudentById(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,num>0?"删除成功":"不存在该学生");
    }

    @ApiOperation("查询学生功能")
    @GetMapping("/select")
    public JsonResult studentSelect(String id){
        return new JsonResult(StatusCode.OPERATION_SUCCESS,studentMapper.selectStudentById(id));
    }

    @ApiOperation("修改学生信息功能")
    @PutMapping("/update")
    public JsonResult studentUpdate(@RequestBody StudentRegDTO studentRegDTO){
        int num = studentMapper.updateStudentById(studentRegDTO);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,num>0?"修改成功,修改了"+num+"条数据":"修改了"+num+"条数据");
    }
}
